﻿using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using WK.Libraries.BetterFolderBrowserNS.Helpers;

namespace RelativeExtensionFiles_Move
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        BetterFolderBrowserDialog _openFolderDialog = null;
        BetterFolderBrowserDialog _saveFolderDialog = null;
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var extensionName = text.Text.Trim();
            var folderOpenPath = textOpenPath.Text.Trim();
            var fileSavePath = textSavePath.Text.Trim();
            if (string.IsNullOrWhiteSpace(extensionName))
            {
                MessageBox.Show("请输入文件后缀");
            }
            else if (string.IsNullOrWhiteSpace(folderOpenPath))
            {
                MessageBox.Show("请选择打开文件夹路径");
            }
            else if (string.IsNullOrWhiteSpace(fileSavePath))
            {
                MessageBox.Show("请选择文件保存路径");
            }
            Task.Run(new Action(() =>
            {
                TraverseDirectory("." + extensionName, folderOpenPath, fileSavePath);
                Dispatcher.Invoke(new Action(() => {
                    MessageBox.Show("完成", "恭喜");
                }));
            }));
        }

        private void TraverseDirectory(string extensionName,string folderOpenPath,string fileSavePath)
        {
            try
            {
                // 遍历文件夹
                DirectoryInfo directory = new DirectoryInfo(folderOpenPath);
                FileSystemInfo[] fileSystemObjects = directory.GetFileSystemInfos();

                foreach (FileSystemInfo fileSystemInfo in fileSystemObjects)
                {
                    if (fileSystemInfo is DirectoryInfo)
                    {
                        // 递归遍历子文件夹
                        TraverseDirectory(extensionName, fileSystemInfo.FullName,fileSavePath);
                    }
                    else
                    {
                        var destFilePath = Path.Combine(fileSavePath, fileSystemInfo.Name);
                        if(fileSystemInfo.Extension == extensionName)
                        {
                            File.Copy(fileSystemInfo.FullName, destFilePath, true);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误");
            }
        }

        private void textOpenPath_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (_openFolderDialog == null)
            {
                _openFolderDialog = new BetterFolderBrowserDialog();
                _openFolderDialog.Title = "选择目标文件夹";
                _openFolderDialog.InitialDirectory = "C:\\";
            }
            var result = _openFolderDialog.ShowDialog();
            if (result)
            {
                this.textOpenPath.Text = _openFolderDialog.FileName;
            }
        }

        private void textSavePath_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (_saveFolderDialog == null)
            {
                _saveFolderDialog = new BetterFolderBrowserDialog();
                _saveFolderDialog.Title = "选择保存文件夹";
                _saveFolderDialog.InitialDirectory = "C:\\";
            }
            var result = _saveFolderDialog.ShowDialog();
            if (result)
            {
                this.textSavePath.Text = _saveFolderDialog.FileName;
            }
        }
    }
}